package com.emergency.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.emergency.data.entity.Duties;
import com.emergency.data.entity.ExpertGroup;
import com.emergency.data.entity.Industry;
import com.emergency.data.vo.ExpertGroupVO;
import com.emergency.data.vo.PageVO;
import com.emergency.mapper.DutiesMapper;
import com.emergency.mapper.ExpertGroupMapper;
import com.emergency.mapper.IndustryMapper;
import com.emergency.service.ExpertGroupService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (ExpertGroup)专家组服务实现类
 *
 * @author makejava
 * @since 2020-09-21
 */
@Service
public class ExpertGroupServiceImpl extends ServiceImpl<ExpertGroupMapper, ExpertGroup> implements ExpertGroupService {

    @Resource
    private ExpertGroupMapper expertGroupMapper;

    @Resource
    private DutiesMapper dutiesMapper;

    @Resource
    private IndustryMapper industryMapper;

    @Override
    public Dict getExpertGroupList(Long dutiesId, Long industryId, String name, Integer page, Integer size) {
        //返回数据
        Dict dict = new Dict();
        //查询条件
        QueryWrapper<ExpertGroup> query = new QueryWrapper<>();
        //从事行业及特长
        if (industryId != null) {
            query.eq("industry_id", industryId);
        }
        //姓名
        if (StrUtil.isNotEmpty(name)) {
            query.like("name", name);
        }
        //专家列表
        List<ExpertGroup> groupList;
        //判断分页情况
        if (page == null) {
            //不分页列表
            groupList = expertGroupMapper.selectAll(dutiesId, industryId, name);
        } else {
            //分页列表
            IPage<ExpertGroup> pageList = expertGroupMapper.selectAllByPage(new Page<>(page, size),dutiesId, industryId, name);
            groupList = pageList.getRecords();
            //分页参数
            PageVO vo = new PageVO();
            vo.setPage(page);
            vo.setSize(groupList.size());
            vo.setTotal(pageList.getTotal());
            dict.put("pageInfo", vo);
        }
        List<ExpertGroupVO> groupVOS = groupList.stream().map(c -> {
            ExpertGroupVO vo = new ExpertGroupVO();
            BeanUtil.copyProperties(c, vo);
            //职称
            Duties duties = dutiesMapper.selectById(c.getDutiesId());
            if (duties != null) {
                vo.setDutiesName(duties.getDutiesName());
            }
            //查询从事行业和特长
            Industry industry = industryMapper.selectById(c.getIndustryId());
            if (industry != null) {
                vo.setIndustryName(industry.getIndustryName());
            }
            return vo;
        }).collect(Collectors.toList());
        dict.put("groupList", groupVOS);
        return dict;
    }
}
